﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Security;
using System.Data;
using System.Text;

using SmartProject.BusinessObjects;

namespace SmartProject.BusinessRules
{
    public class RemoveUser : BusinessRule
    {
        public override string Name
        {
            get { return ""; }
        }

        public override string Description
        {
            get { return "Prerequsites for removing a user as well as actions to perform when removing a user."; }
        }

        private string BuildLastErrorMessage(DataTable works)
        {
            StringBuilder text = new StringBuilder();
            text.AppendLine("Please update the following work items with a different owner/developer before removing this user.");
            for (int i = 0; i < works.Rows.Count; i++)
            {
                text.AppendLine(String.Format("Work Id #{0} is still associated with this user.",
                    works.Rows[i]["WorkId"].ToString()));
            }
            return text.ToString();
        }

        public override void Implement(Entity data)
        {
            if (data.StoredProc == "smartProject_RemoveUser")
            {
                // First, we make sure that the logged in user is not removing him/herself.
                int userId = (int)data.GetBusinessRuleParameterValue("UserId");
                int loggedInUserId = (int)data.GetBusinessRuleParameterValue("LoggedInUserId");

                if (userId == loggedInUserId)
                {
                    throw new Exceptions.BusinessRuleViolation("You cannot remove yourself.");
                }

                // Second, we determine if the user is associated with any
                // work items.
                Work work = new Work();
                /*
                work.SetAttribute("Criteria", "OwnerWorks");
                work.SetAttribute("Id", userId);
                */
                string conditional = String.Format(" [UserId] = {0} ", userId);
                DataTable works = work.LoadWorks(conditional, "Created", true);
                if (works != null && works.Rows.Count > 0)
                {
                    throw new Exceptions.BusinessRuleViolation(BuildLastErrorMessage(works));
                }
                conditional = String.Format(" [DeveloperId] = {0} ", userId);
                //work.SetAttribute("Criteria", "DeveloperWorks");
                //work.SetAttribute("Id", userId);
                works = work.LoadWorks(conditional, "Created", true);
                if (works != null && works.Rows.Count > 0)
                {
                    throw new Exceptions.BusinessRuleViolation(BuildLastErrorMessage(works));
                }
            }
        }

        public override bool QueryBooleanProperty(string propertyName)
        {
            return true;
        }
    }
}
